package com.wuyu.mini.dubbo.cluster.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 配置工具类
 * @author wuyu
 */
public class ConfigUtil {

    private volatile static ConfigUtil instance = null;
    private Properties property;

    public static ConfigUtil getInstance() {
        if (instance == null) {
            synchronized (ConfigUtil.class) {
                if (instance == null) {
                    instance = new ConfigUtil();
                }
            }
        }
        return instance;
    }

    private ConfigUtil() {
        initProperty();
    }

    private void initProperty() {
        InputStream inputStream = null;
        try {
            inputStream = getClass().getClassLoader().getResourceAsStream("dubbo.properties");
            property = new Properties();
            property.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(String.format("call PropertyUtil.initProperty, e.getMessage:[%s]", e.getMessage()), e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(String.format("call PropertyUtil.initProperty, e.getMessage:[%s]", e.getMessage()), e);
                }
            }
        }
    }

    public String get(String key) {
        String property = this.property.getProperty(key);
        return property;
    }
}
